package com.spzx;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.security.annotation.EnableCustomConfig;
import com.spzx.common.security.annotation.EnableRyFeignClients;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.service.ProductSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

@EnableCustomConfig
@EnableRyFeignClients
@SpringBootApplication
public class SpzxProductApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(SpzxProductApplication.class,args);
    }

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ProductSkuService productSkuService;
    @Override
    public void run(String... args) throws Exception {
        LambdaQueryWrapper<ProductSku> wrapper = new LambdaQueryWrapper<>();
        //查询所有上架商品的id放到bitmap位图中
        LambdaQueryWrapper<ProductSku> select = wrapper.eq(ProductSku::getStatus, 1).select(ProductSku::getId);
        //设置位图数据的key
        String key = "product:sku:data";
        List<ProductSku> productSkus = productSkuService.list(select);
        productSkus.forEach(productSku -> redisTemplate.opsForValue().setBit(key,productSku.getId(),true));
    }
}
